package models

import "time"

type InterPushReport struct {
	ID       int `gorm:"primary_key"`
	UserId int
	MatchId int
	Type int
	Item string
	Supplement string
	Status int
	SolveUserId int
	Operation string
	Replay string
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (i *InterPushReport) TableName() string {
	return "interpush_report"
}

type InterPushReportResult struct {
	Id        int `json:"id"`
	Phone     string `json:"phone"`
	Informant string `json:"informant"`
	Criminal  string `json:"criminal"`
	Item      string `json:"item"`
	CreatedAt time.Time `json:"created_at"`
	Status    int `json:"status"`
	Type int `json:"type"`
}

func GetInterPushById(id string) *InterPushReport{
	var res InterPushReport
	DB.Model(&InterPushReport{}).Where("id=?",id).First(&res)
	return &res
}

func GetInterPushReport(item string, status string, criminal string, beginTime string, endTime string, page int, pageSize int) ([]*InterPushReportResult, *Paginator){
	var result []*InterPushReportResult
	report := DB.Table("interpush_report as r").Select("r.id,r.type,us.phone,u.wechat_name as informant,u1.wechat_name as criminal ,r.item,r.created_at,r.status").Joins("left join users as us on us.id=r.user_id").Joins("left join user_detail as u on u.user_id=r.user_id").Joins("left join user_detail as u1 on u1.user_id=r.match_id")
	if item != ""{
		report = report.Where("r.item like ? and type=1", "%"+item+"%")
	}
	if status != "" {
		report = report.Where("r.status=?", status)
	}
	if criminal != "" {
		report = report.Where("u1.wechat_name like ?", "%"+criminal+"%")
	}
	if beginTime != "" {
		report = report.Where("r.created_at > ?", beginTime)
	}
	if endTime != "" {
		report = report.Where("r.created_at < ?", endTime)
	}
	if item == "对方未分享" {
		report = report.Where("r.type=0")
	}
	var count int
	report.Count(&count)
	paginate := NewPage(page, pageSize, count)
	err := report.Offset(paginate.Offset).Limit(paginate.Limit).Order("r.created_at desc").Find(&result).Error
	if err != nil {
		panic("错误")
	}
	return result, &paginate
}

type InterPushReportOne struct {
	ID int `json:"id"`
	Phone string `json:"phone"`
	Name string `json:"name"`
	Target string `json:"target"`
	CreatedAt time.Time `json:"created_at"`
	Quote string `json:"quote"`
	Type string `json:"type"`
	Content string `json:"content"`
	Status int `json:"status"`
	Operation string `json:"operation"`
	Replay string `json:"replay"`
}

func InterPushReportGet(id int) *InterPushReportOne {
	var res InterPushReport

	DB.Table("interpush_report").Where("id=?",id).First(&res)
	var name string
	DB.Table("user_detail").Select("wechat_name").Where("user_id=?", res.UserId).Row().Scan(&name)
	var phone string
	DB.Table("users").Select("phone").Where("id=?",res.UserId).Row().Scan(&phone)
	var target string
	DB.Table("user_detail").Select("wechat_name").Where("user_id=?", res.MatchId).Row().Scan(&target)

	return &InterPushReportOne{res.ID,phone,name,target,res.CreatedAt,"互推圈",res.Item,res.Supplement,res.Status,res.Operation,res.Replay}
}